package com.kaibes.platform.article.db.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.kaibes.platform.article.db.entity.ArticleTag;
import com.kaibes.web.api.ResponseServiceImpl;

@Service
public class ArticleTagService extends ResponseServiceImpl<ArticleTag> {
    
    public boolean saveIfNotExist(List<String> tagList) {
        List<ArticleTag> articleTagList = list("name");
        List<ArticleTag> addThis = new ArrayList<>();
        for (int i = 0; i < tagList.size(); i++) {
            String tag = tagList.get(i);
            boolean flag = true;
            for (ArticleTag articleTag : articleTagList) {
                if (articleTag.getName().equals(tag)) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                ArticleTag articleTag = new ArticleTag();
                articleTag.setName(tag);
                addThis.add(articleTag);
            }
        }
        if (!addThis.isEmpty()) {
        	return save(addThis)!=null;
        }
        return true;
    }
    public boolean saveIfNotExist(String[] tags) {
        List<ArticleTag> articleTagList = list("name");
        List<ArticleTag> addThis = new ArrayList<>();
        for (String tag : tags) {
            if (tag.isEmpty()) {
                continue;
            }
            
            boolean flag = true;
            for (ArticleTag articleTag : articleTagList) {
                if (articleTag.getName().equals(tag)) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                ArticleTag articleTag = new ArticleTag();
                articleTag.setName(tag);
                addThis.add(articleTag);
            }
        }
        return save(addThis)!=null;
    }
}
